package com.sali.java_stream;

import com.sali.pojo.model.Apple;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 匿名内部类是一种允许你随建随用，创建对象和覆盖行为方法的一种机制
 * @date: 2023/3/12 12:31.
 * @version: 1.0
 */
@SpringBootTest
public class LambdaApplication {

    List<Apple> apples;

    {
        apples = new ArrayList<>();
        apples.add(new Apple(1, "红色", "红色苹果", 220));
        apples.add(new Apple(2, "绿色", "绿色苹果", 400));
        apples.add(new Apple(3, "浅绿色", "浅绿色苹果", 210));
    }


    @Test
    void appleFilter() {
        // 过滤红色的苹果
        List<Apple> filterAppleList = filterAppleColorAndWeight(apples, apple -> apple.getColor().equals("绿色"));
        filterAppleList.forEach(System.out::println);
    }


    private List<Apple> filterAppleColorAndWeight(List<Apple> apples, AppPredicate appPredicate) {
        List<Apple> resultList = new ArrayList<>();
        for (Apple apple : apples) {
            if (appPredicate.test(apple)) {
                resultList.add(apple);
            }
        }
        return resultList;
    }

    public interface AppPredicate {

        boolean test(Apple apple);
    }


}
